package coms.action;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import net.sf.jasperreports.engine.JasperCompileManager;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import coms.datatransfer.FileItemDTO;
import coms.datatransfer.ReviewDTO;
import coms.ejb.sessionbean.FileBeanRemote;
import coms.ejb.sessionbean.ReviewBeanRemote;
import coms.service.BusinessDelegate;

public class JasperAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	
	private BusinessDelegate delegate;
	private ReviewBeanRemote reviewBean;
	private FileBeanRemote fileBean;
	
	private String userId;
	private String fileId;
	
	private String sourceFile = null;
	private String jasperFile = null;
	
	/** List to use as our JasperReports dataSource. */
	private List<ReviewDTO> reviews;
	private FileItemDTO file;
	
	public JasperAction(){
		delegate = new BusinessDelegate();
		reviewBean = (ReviewBeanRemote)delegate.getBean("reviewBean");
		fileBean = (FileBeanRemote)delegate.getBean("fileBean");
	}
	
	public void setUserId(String userId){this.userId = userId;}    
    public String getUserId(){return this.userId;}
    public void setFileId(String fileId){this.fileId = fileId;}
    public String getFileId(){return this.fileId;}
	
    public List<ReviewDTO> getReviews(){return this.reviews;}
    public FileItemDTO getFile(){return this.file;}
    
	public String getSourceFile(){return this.sourceFile;}	
	public void setSourceFile(String sourceFile){this.sourceFile = sourceFile;}	
	public String getJasperFile(){return this.jasperFile;}	
	public void setJasperFile(String jasperFile){this.jasperFile = jasperFile;}
	
	public String execute() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		Map<String,Object> session = ActionContext.getContext().getSession();
		
		fileId = request.getParameter("fileID");
		userId = ((Integer)session.get("userID")).toString();
		System.out.println("inside action userId: " + userId);
		reviews = new ArrayList<ReviewDTO>();
				
		/*if(userId.length() != 0){
			reviews = (List<ReviewDTO>)reviewBean.displayReviewedFiles(Integer.parseInt(userId));
		}else{
			reviews = (List<ReviewDTO>)bean.displayReviewedFiles();
		}*/
		reviews = (List<ReviewDTO>)reviewBean.displayReviewedFiles(Integer.parseInt(userId));
		file = fileBean.findFileItemById(Integer.parseInt(fileId));
		System.out.println("reviews list: " + reviews.size()); 
		
		//sourceFile = ServletActionContext.getServletContext().getRealPath("our_jasper_template.jrxml");
		jasperFile = ServletActionContext.getServletContext().getRealPath("our_compiled_template.jasper");
			
		try {
            JasperCompileManager.compileReportToFile("D:/jasper/our_jasper_template.jrxml", jasperFile);
			
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
		
		return "success";
		
	}
}
